// pages/me/me.js
var http = require("../../utils/http.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //用户个人信息
    userInfo: {
      avatarUrl: "/static/image/me/users.png", //用户头像
      nickName: "", //用户昵称
    },
    is_login: 0
  },
  login() {
    let that = this;
    wx.login({
      success(res) {

        let data = {
          code: res.code
        };
        http.request('/wechatshop/we_chat_auth', data).then(data => {
          wx.setStorageSync('is_login', data.is_login);
          that.setData({
            'is_login': data.is_login
          });
          wx.getUserInfo({
            success: function(res) {
              // console.log(res);
              that.setData({
                'userInfo.avatarUrl': res.userInfo.avatarUrl,
                'userInfo.nickName': res.userInfo.nickName
              });

              let values = {
                'nick_name': res.userInfo.nickName,
                "gender": res.userInfo.gender,
                "avatar_url": res.userInfo.avatarUrl,
                "province": res.userInfo.province,
                "city": res.userInfo.city,
                "country": res.userInfo.country,
              };
              let param = {
                model: 'wechatshop.user',
                domain: JSON.stringify([
                  ['id', '=', wx.getStorageSync('is_login')]
                ]),
                values: JSON.stringify(values)
              }
              http.request('/wechatshop/api/object/update', param).then(data => {

              });

              wx.setStorage({
                key: 'avatarUrl',
                data: res.userInfo.avatarUrl,
              });
              wx.setStorage({
                key: 'nickName',
                data: res.userInfo.nickName,
              });

            }
          });
        });
      }

    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    let is_login = wx.getStorageSync('is_login');
    if (is_login && is_login != 0) {
      this.setData({
        'is_login': Number.parseInt(is_login),
        'userInfo.avatarUrl': wx.getStorageSync('avatarUrl'),
        'userInfo.nickName': wx.getStorageSync('nickName')
      });

    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})